<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  
  <title>1.reactivity | 个人博客</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta name="description" content="shared&#x2F;src&#x2F;index.ts 123456export const isObject &#x3D; (value) &#x3D;&gt;&amp;#123;    return typeof value &#x3D;&#x3D;&#x3D; &amp;#x27;object&amp;#x27; &amp;&amp; value !&#x3D;&#x3D; null&amp;#125;export const isFunction&#x3D; (value) &#x3D;&gt;&amp;#12">
<meta property="og:type" content="article">
<meta property="og:title" content="1.reactivity">
<meta property="og:url" content="https://gitee.com/wieldy/bailment-blog/2022/03/29/vue3-1.reactivity/index.html">
<meta property="og:site_name" content="个人博客">
<meta property="og:description" content="shared&#x2F;src&#x2F;index.ts 123456export const isObject &#x3D; (value) &#x3D;&gt;&amp;#123;    return typeof value &#x3D;&#x3D;&#x3D; &amp;#x27;object&amp;#x27; &amp;&amp; value !&#x3D;&#x3D; null&amp;#125;export const isFunction&#x3D; (value) &#x3D;&gt;&amp;#12">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2022-03-29T14:12:00.000Z">
<meta property="article:modified_time" content="2023-04-23T12:11:43.652Z">
<meta property="article:author" content="John Doe">
<meta name="twitter:card" content="summary">
  
    <link rel="alternate" href="/bailment-blog/atom.xml" title="个人博客" type="application/atom+xml">
  
  
    <link rel="shortcut icon" href="/bailment-blog/favicon.png">
  
  
    
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/typeface-source-code-pro@0.0.71/index.min.css">

  
  
<link rel="stylesheet" href="/bailment-blog/css/style.css">

  
    
<link rel="stylesheet" href="/bailment-blog/fancybox/jquery.fancybox.min.css">

  
<meta name="generator" content="Hexo 6.3.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/bailment-blog/" id="logo">个人博客</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/bailment-blog/">Home</a>
        
          <a class="main-nav-link" href="/bailment-blog/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/bailment-blog/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="Search"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="https://gitee.com/wieldy/bailment-blog"></form>
      </div>
    </div>
  </div>
</header>

      <div class="outer">
        <section id="main"><article id="post-vue3-1.reactivity" class="h-entry article article-type-post" itemprop="blogPost" itemscope itemtype="https://schema.org/BlogPosting">
  <div class="article-meta">
    <a href="/bailment-blog/2022/03/29/vue3-1.reactivity/" class="article-date">
  <time class="dt-published" datetime="2022-03-29T14:12:00.000Z" itemprop="datePublished">2022-03-29</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="p-name article-title" itemprop="headline name">
      1.reactivity
    </h1>
  

      </header>
    
    <div class="e-content article-entry" itemprop="articleBody">
      
        <ul>
<li><p>shared&#x2F;src&#x2F;index.ts</p>
<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">isObject</span> = (<span class="params">value</span>) =&gt;&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">typeof</span> value === <span class="string">&#x27;object&#x27;</span> &amp;&amp; value !== <span class="literal">null</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="title function_">isFunction</span>= (<span class="params">value</span>) =&gt;&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">typeof</span> value === <span class="string">&#x27;function&#x27;</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>基本使用</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;!DOCTYPE <span class="keyword">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">html</span> <span class="attr">lang</span>=<span class="string">&quot;en&quot;</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">head</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">&quot;UTF-8&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">meta</span> <span class="attr">http-equiv</span>=<span class="string">&quot;X-UA-Compatible&quot;</span> <span class="attr">content</span>=<span class="string">&quot;IE=edge&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">meta</span> <span class="attr">name</span>=<span class="string">&quot;viewport&quot;</span> <span class="attr">content</span>=<span class="string">&quot;width=device-width, initial-scale=1.0&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">title</span>&gt;</span>Document<span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line">    <span class="comment">&lt;!-- &lt;script src=&quot;../../../node_modules/@vue/reactivity/dist/reactivity.global.js&quot;&gt;&lt;/script&gt; --&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">script</span> <span class="attr">src</span>=<span class="string">&quot;./reactivity.global.js&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">&quot;app&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">script</span>&gt;</span><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">        <span class="comment">// 缓存代理结果 require()</span></span></span><br><span class="line"><span class="language-javascript">        <span class="keyword">const</span> &#123;effect ,reactive&#125; = <span class="title class_">VueReactivity</span>;</span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">        <span class="comment">// 会对属性进行劫持 proxy， 监听用户的获取操作和设置操作</span></span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">        <span class="keyword">const</span> obj = &#123;<span class="attr">name</span>:<span class="string">&#x27;zf&#x27;</span>,<span class="attr">age</span>:<span class="number">13</span>, <span class="attr">address</span>:&#123;<span class="attr">num</span>:<span class="number">30</span>&#125;,<span class="attr">flag</span>:<span class="literal">true</span>&#125;</span></span><br><span class="line"><span class="language-javascript">        <span class="keyword">const</span> state = <span class="title function_">reactive</span>(obj); <span class="comment">// 只能传入对象， 因为proxy 只支持对象格式</span></span></span><br><span class="line"><span class="language-javascript">        </span></span><br><span class="line"><span class="language-javascript">        <span class="comment">// effect 函数默认会执行一次， 后续数据变化了会重新执行effect函数</span></span></span><br><span class="line"><span class="language-javascript">        <span class="title function_">effect</span>(<span class="function">()=&gt;</span>&#123;</span></span><br><span class="line"><span class="language-javascript">            app.<span class="property">innerHTML</span> = state.<span class="property">name</span> + <span class="string">&#x27;今年&#x27;</span> + state.<span class="property">age</span> + <span class="string">&#x27;岁了门牌号是 &#x27;</span>+ state.<span class="property">address</span>.<span class="property">num</span></span></span><br><span class="line"><span class="language-javascript">        &#125;)</span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">        <span class="built_in">setTimeout</span>(<span class="function">()=&gt;</span>&#123;</span></span><br><span class="line"><span class="language-javascript">            state.<span class="property">age</span>++</span></span><br><span class="line"><span class="language-javascript">        &#125;,<span class="number">1000</span>)</span></span><br><span class="line"><span class="language-javascript">        <span class="comment">// effect()  -&gt; effect()</span></span></span><br><span class="line"><span class="language-javascript">    </span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>reactivity</p>
<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// reactive.ts</span></span><br><span class="line"><span class="keyword">import</span> &#123; isObject &#125; <span class="keyword">from</span> <span class="string">&quot;@vue/shared&quot;</span>;</span><br><span class="line"><span class="keyword">import</span>  &#123; <span class="title class_">ReactiveFlags</span>,baseHandler &#125; <span class="keyword">from</span> <span class="string">&quot;./baseHandler&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> reactiveMap = <span class="keyword">new</span> <span class="title class_">WeakMap</span>();<span class="comment">// key必须是对象，弱引用</span></span><br><span class="line"><span class="comment">// v8的垃圾回收机制  标记删除  引用计数</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">function</span> <span class="title function_">reactive</span>(<span class="params">target</span>)&#123;</span><br><span class="line">    <span class="keyword">if</span>(!<span class="title function_">isObject</span>(target))&#123;</span><br><span class="line">        <span class="keyword">return</span> target</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// es6 中的proxy </span></span><br><span class="line">    <span class="keyword">if</span>(target[<span class="title class_">ReactiveFlags</span>.<span class="property">IS_REACTIVE</span>])&#123;</span><br><span class="line">        <span class="keyword">return</span> target</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">const</span> existing = reactiveMap.<span class="title function_">get</span>(target);</span><br><span class="line">    <span class="keyword">if</span>(existing)&#123;</span><br><span class="line">        <span class="keyword">return</span> existing</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">const</span> proxy = <span class="keyword">new</span> <span class="title class_">Proxy</span>(target,baseHandler);</span><br><span class="line">    reactiveMap.<span class="title function_">set</span>(target,proxy)</span><br><span class="line">    <span class="keyword">return</span> proxy;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 一个对象已经被代理过了 ，就不要再次被代理了</span></span><br><span class="line"><span class="comment">// 使用proxy 要搭配Reflect来使用  obj -&gt; proxy</span></span><br><span class="line"><span class="comment">// 下次你传入的是proxy了，我去这个对象上取值可以命中proxy的get方法</span></span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">let person = &#123;</span></span><br><span class="line"><span class="comment">    name:&#x27;zf&#x27;,</span></span><br><span class="line"><span class="comment">    get aliasName()&#123; // 属性访问器</span></span><br><span class="line"><span class="comment">        return this.name + &#x27;jg&#x27;</span></span><br><span class="line"><span class="comment">    &#125;</span></span><br><span class="line"><span class="comment">&#125;</span></span><br><span class="line"><span class="comment">// proxy 一般搭配Reflect来使用</span></span><br><span class="line"><span class="comment">const proxy = new Proxy(person,&#123;</span></span><br><span class="line"><span class="comment">    get(target,key,receiver)&#123;</span></span><br><span class="line"><span class="comment">        console.log(&#x27;这里可以记录这个属性使用了哪个effect&#x27;)</span></span><br><span class="line"><span class="comment">        return Reflect.get(target,key,receiver)</span></span><br><span class="line"><span class="comment">    &#125;,</span></span><br><span class="line"><span class="comment">    set(target,key,value,receiver)&#123;</span></span><br><span class="line"><span class="comment">        console.log(&#x27;这里可以通知effect重新执行&#x27;)</span></span><br><span class="line"><span class="comment">        return Reflect.set(target,key,value,receiver);</span></span><br><span class="line"><span class="comment">    &#125;</span></span><br><span class="line"><span class="comment">&#125;);</span></span><br><span class="line"><span class="comment">// 原因是因为我去proxy上取aliasName,这个时候回执行get方法</span></span><br><span class="line"><span class="comment">// 但是aliasName 是基于name属性 原则上应该去name上取值</span></span><br><span class="line"><span class="comment">// 然而this.name 并没有触发proxy的get，也就意味着稍后我们修改name属性的时候</span></span><br><span class="line"><span class="comment">// 就不会导致页面重新渲染</span></span><br><span class="line"><span class="comment">proxy.aliasName</span></span><br><span class="line"><span class="comment">*/</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>baseHandler</p>
<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// baseHandler.ts</span></span><br><span class="line"><span class="keyword">import</span> &#123; isObject &#125; <span class="keyword">from</span> <span class="string">&quot;@vue/shared&quot;</span>;</span><br><span class="line"><span class="keyword">import</span> &#123; reactive &#125; <span class="keyword">from</span> <span class="string">&quot;./reactive&quot;</span>;</span><br><span class="line"><span class="keyword">import</span> &#123; track, trigger &#125; <span class="keyword">from</span> <span class="string">&quot;./effect&quot;</span>;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> <span class="keyword">enum</span> <span class="title class_">ReactiveFlags</span> &#123;</span><br><span class="line">    <span class="variable constant_">IS_REACTIVE</span> = <span class="string">&#x27;__v_isReactive&#x27;</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">const</span> baseHandler  = &#123;</span><br><span class="line">    <span class="title function_">get</span>(<span class="params">target,key,receiver</span>)&#123;</span><br><span class="line">        <span class="keyword">if</span>(key === <span class="title class_">ReactiveFlags</span>.<span class="property">IS_REACTIVE</span>)&#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="literal">true</span></span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">// 让当前的key 和 effect关联起来即可</span></span><br><span class="line">        <span class="title function_">track</span>(target,key);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// lazy proxy</span></span><br><span class="line">        <span class="keyword">let</span> res =  <span class="title class_">Reflect</span>.<span class="title function_">get</span>(target,key,receiver);</span><br><span class="line">        <span class="keyword">if</span>(<span class="title function_">isObject</span>(res))&#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="title function_">reactive</span>(res) </span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> res</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="title function_">set</span>(<span class="params">target,key,value,receiver</span>)&#123;</span><br><span class="line">        <span class="comment">// 数据变化后，要根据属性找到对应的effect列表让其依次执行</span></span><br><span class="line">        <span class="keyword">let</span> oldValue = target[key];</span><br><span class="line">        <span class="keyword">if</span>(oldValue !== value )&#123;</span><br><span class="line">            <span class="keyword">let</span> result = <span class="title class_">Reflect</span>.<span class="title function_">set</span>(target,key,value,receiver);;</span><br><span class="line">            <span class="title function_">trigger</span>(target,key,value);</span><br><span class="line">            <span class="keyword">return</span> result</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p>src&#x2F; 目录下</p>
<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// index.ts</span></span><br><span class="line"><span class="keyword">export</span> &#123;effect&#125; <span class="keyword">from</span> <span class="string">&#x27;./effect&#x27;</span>;</span><br><span class="line"><span class="keyword">export</span> &#123;reactive&#125; <span class="keyword">from</span> <span class="string">&#x27;./reactive&#x27;</span>;</span><br><span class="line"><span class="keyword">export</span> &#123;computed&#125; <span class="keyword">from</span> <span class="string">&#x27;./computed&#x27;</span>;</span><br></pre></td></tr></table></figure>
</li>
<li><p>effect.ts</p>
<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">let</span> activeEffect = <span class="literal">undefined</span>;</span><br><span class="line"><span class="comment">// 依赖收集的原理是 借助js是单线程的特点， 默认调用effect的时候会去调用proxy的get，此时让属性记住</span></span><br><span class="line"><span class="comment">// 依赖的effect，同理也让effect记住对应的属性</span></span><br><span class="line"><span class="comment">// 靠的是数据结构 weakMap : &#123;map:&#123;key:new Set()&#125;&#125;</span></span><br><span class="line"><span class="comment">// 稍后数据变化的时候 找到对应的map 通过属性出发set中effect</span></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">cleanEffect</span>(<span class="params">effect</span>) &#123;</span><br><span class="line">    <span class="comment">// 需要清理effect中存入属性中的set中的effect </span></span><br><span class="line">    <span class="comment">// 每次执行前都需要将effect只对应属性的set集合都清理掉</span></span><br><span class="line">    <span class="comment">// 属性中的set 依然存放effect</span></span><br><span class="line">    <span class="keyword">let</span> deps = effect.<span class="property">deps</span></span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">0</span>; i &lt; deps.<span class="property">length</span>; i++) &#123;</span><br><span class="line">        deps[i].<span class="title function_">delete</span>(effect)</span><br><span class="line">    &#125;</span><br><span class="line">    effect.<span class="property">deps</span>.<span class="property">length</span> = <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">class</span> <span class="title class_">ReactiveEffect</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> active = <span class="literal">true</span>;</span><br><span class="line">    <span class="keyword">public</span> parent = <span class="literal">null</span>;</span><br><span class="line">    <span class="keyword">public</span> deps = []; <span class="comment">// effect中用了哪些属性，后续清理的时候要使用</span></span><br><span class="line">    <span class="title function_">constructor</span>(<span class="params"><span class="keyword">public</span> fn,<span class="keyword">public</span> scheduler?</span>) &#123; &#125; <span class="comment">// 你传递的fn我会帮你放到this上</span></span><br><span class="line">    <span class="comment">// effectScope 可以来实现让所有的effect停止</span></span><br><span class="line">    <span class="title function_">run</span>(<span class="params"></span>) &#123;</span><br><span class="line">        <span class="comment">// 依赖收集  让属性和effect 产生关联</span></span><br><span class="line">        <span class="keyword">if</span> (!<span class="variable language_">this</span>.<span class="property">active</span>) &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="variable language_">this</span>.<span class="title function_">fn</span>();</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="keyword">try</span> &#123;</span><br><span class="line">                <span class="variable language_">this</span>.<span class="property">parent</span> = activeEffect</span><br><span class="line">                activeEffect = <span class="variable language_">this</span>;</span><br><span class="line">                <span class="title function_">cleanEffect</span>(<span class="variable language_">this</span>); <span class="comment">// vue2 和 vue3中都是要清理的 </span></span><br><span class="line">                <span class="keyword">return</span> <span class="variable language_">this</span>.<span class="title function_">fn</span>(); <span class="comment">// 去proxy对象上取值, 取之的时候 我要让这个熟悉 和当前的effect函数关联起来，稍后数据变化了 ，可以重新执行effect函数</span></span><br><span class="line">            &#125; <span class="keyword">finally</span> &#123;</span><br><span class="line">                <span class="comment">// 取消当前正在运行的effect</span></span><br><span class="line">                activeEffect = <span class="variable language_">this</span>.<span class="property">parent</span>;</span><br><span class="line">                <span class="variable language_">this</span>.<span class="property">parent</span> = <span class="literal">null</span>;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="title function_">stop</span>(<span class="params"></span>) &#123;</span><br><span class="line">        <span class="keyword">if</span> (<span class="variable language_">this</span>.<span class="property">active</span>) &#123;</span><br><span class="line">            <span class="variable language_">this</span>.<span class="property">active</span> = <span class="literal">false</span>;</span><br><span class="line">            <span class="title function_">cleanEffect</span>(<span class="variable language_">this</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 哪个对象中的那个属性 对应的哪个effect  一个属性可以对应多个effect</span></span><br><span class="line"><span class="comment">// 外层用一个map &#123;object: &#123;name:[effect,effect],age:[effect,effect]&#125;&#125;</span></span><br><span class="line"><span class="keyword">const</span> targetMap = <span class="keyword">new</span> <span class="title class_">WeakMap</span>();</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">function</span> <span class="title function_">trigger</span>(<span class="params">target, key, value</span>) &#123;</span><br><span class="line">    <span class="keyword">let</span> depsMap = targetMap.<span class="title function_">get</span>(target);</span><br><span class="line">    <span class="keyword">if</span> (!depsMap) &#123;</span><br><span class="line">        <span class="keyword">return</span>; <span class="comment">// 属性没有依赖任何的effect</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">let</span> effects = depsMap.<span class="title function_">get</span>(key)</span><br><span class="line">    <span class="title function_">triggerEffects</span>(effects)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">function</span> <span class="title function_">triggerEffects</span>(<span class="params">effects</span>)&#123;</span><br><span class="line">    <span class="keyword">if</span> (effects) &#123;</span><br><span class="line">        effects = <span class="keyword">new</span> <span class="title class_">Set</span>(effects)</span><br><span class="line">        effects.<span class="title function_">forEach</span>(<span class="function"><span class="params">effect</span> =&gt;</span> &#123;</span><br><span class="line">            <span class="keyword">if</span> (effect !== activeEffect) &#123; <span class="comment">// 保证要执行的effect不是当前的effect</span></span><br><span class="line">                <span class="keyword">if</span>(effect.<span class="property">scheduler</span>)&#123;</span><br><span class="line">                    effect.<span class="title function_">scheduler</span>(); <span class="comment">// 可以提供一个调度函数，用户实现自己的逻辑</span></span><br><span class="line">                &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">                    effect.<span class="title function_">run</span>(); <span class="comment">// 数据变化了，找到对应的effect 重新执行</span></span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">function</span> <span class="title function_">track</span>(<span class="params">target, key</span>) &#123;</span><br><span class="line">    <span class="keyword">if</span> (activeEffect) &#123;</span><br><span class="line">        <span class="comment">// 这里搞依赖收集</span></span><br><span class="line">        <span class="keyword">let</span> depsMap = targetMap.<span class="title function_">get</span>(target);</span><br><span class="line">        <span class="keyword">if</span> (!depsMap) &#123;</span><br><span class="line">            targetMap.<span class="title function_">set</span>(target, (depsMap = <span class="keyword">new</span> <span class="title class_">Map</span>()))</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">let</span> deps = depsMap.<span class="title function_">get</span>(key);</span><br><span class="line">        <span class="keyword">if</span> (!deps) &#123;</span><br><span class="line">            depsMap.<span class="title function_">set</span>(key, (deps = <span class="keyword">new</span> <span class="title class_">Set</span>()))</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="title function_">trackEffects</span>(deps)</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 让属性记录所用到的effect是谁， 哪个effect对应了哪些属性</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">function</span> <span class="title function_">trackEffects</span>(<span class="params">deps</span>)&#123;</span><br><span class="line">    <span class="keyword">let</span> shouldTrack = !deps.<span class="title function_">has</span>(activeEffect);</span><br><span class="line">    <span class="keyword">if</span> (shouldTrack) &#123;</span><br><span class="line">        deps.<span class="title function_">add</span>(activeEffect);</span><br><span class="line">        <span class="comment">// 等等 deps 的作用就是让effect 记录用到了哪些属性</span></span><br><span class="line">        activeEffect.<span class="property">deps</span>.<span class="title function_">push</span>(deps); <span class="comment">// 放的是set </span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">function</span> <span class="title function_">effect</span>(<span class="params">fn, options = &#123;&#125; <span class="keyword">as</span> <span class="built_in">any</span></span>) &#123;</span><br><span class="line">    <span class="comment">// 将用户传递的函数编程响应式的effect</span></span><br><span class="line">    <span class="keyword">const</span> _effect = <span class="keyword">new</span> <span class="title class_">ReactiveEffect</span>(fn,options.<span class="property">scheduler</span>);</span><br><span class="line">    <span class="comment">// 更改runner中的this</span></span><br><span class="line">    _effect.<span class="title function_">run</span>()</span><br><span class="line">    <span class="keyword">const</span> runner = _effect.<span class="property">run</span>.<span class="title function_">bind</span>(_effect);</span><br><span class="line">    runner.<span class="property">effect</span> = _effect; <span class="comment">// 暴露effect的实例</span></span><br><span class="line">    <span class="keyword">return</span> runner<span class="comment">// 用户可以手动调用runner重新执行</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// activeEffect = e2;</span></span><br><span class="line"><span class="comment">// effect(()=&gt;&#123; // e1  e1.parent = null</span></span><br><span class="line"><span class="comment">//     state.name;  // name = e1</span></span><br><span class="line"><span class="comment">//     effect(()=&gt;&#123; // e2  e2.parent = e1;</span></span><br><span class="line"><span class="comment">//         state.age; // age = e2</span></span><br><span class="line"><span class="comment">//     &#125;)</span></span><br><span class="line"><span class="comment">//     // activeeffect = e2.parent</span></span><br><span class="line"><span class="comment">//     state.address; // address = e1</span></span><br><span class="line"><span class="comment">// &#125;)</span></span><br><span class="line"></span><br></pre></td></tr></table></figure></li>
</ul>
<!-- computed -->
<figure class="highlight typescript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// computed.ts</span></span><br><span class="line"><span class="keyword">import</span> &#123; isFunction &#125; <span class="keyword">from</span> <span class="string">&quot;@vue/shared&quot;</span>;</span><br><span class="line"><span class="keyword">import</span> &#123; activeEffect, <span class="title class_">ReactiveEffect</span>, track, trackEffects, triggerEffects &#125; <span class="keyword">from</span> <span class="string">&quot;./effect&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">function</span> <span class="title function_">computed</span>(<span class="params">getterOrOptions</span>)&#123;</span><br><span class="line">    <span class="keyword">let</span> isGetter = <span class="title function_">isFunction</span>(getterOrOptions);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">let</span> getter;</span><br><span class="line">    <span class="keyword">let</span> setter;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">const</span> <span class="title function_">fn</span> = (<span class="params"></span>)=&gt; <span class="variable language_">console</span>.<span class="title function_">warn</span>(<span class="string">&#x27;computed is readonly &#x27;</span>);</span><br><span class="line">    <span class="keyword">if</span>(isGetter)&#123;</span><br><span class="line">        getter = getterOrOptions</span><br><span class="line">        setter = fn</span><br><span class="line">    &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">        getter = getterOrOptions.<span class="property">get</span>;</span><br><span class="line">        setter = getterOrOptions.<span class="property">set</span>  || fn</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">new</span> <span class="title class_">ComputedRefImpl</span>(getter,setter)</span><br><span class="line">    </span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">class</span> <span class="title class_">ComputedRefImpl</span>&#123;</span><br><span class="line">    <span class="keyword">private</span> _value;</span><br><span class="line">    <span class="keyword">private</span> _dirty = <span class="literal">true</span>;</span><br><span class="line">    <span class="keyword">public</span> effect;</span><br><span class="line">    <span class="keyword">public</span> deps;</span><br><span class="line">    <span class="title function_">constructor</span>(<span class="params">getter,<span class="keyword">public</span> setter</span>)&#123;</span><br><span class="line">        <span class="comment">// 拿到effect实例让函数执行</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">// effect.deps 存的是属性对应的set集合</span></span><br><span class="line">       <span class="variable language_">this</span>.<span class="property">effect</span> = <span class="keyword">new</span> <span class="title class_">ReactiveEffect</span>(getter,<span class="function">()=&gt;</span>&#123;</span><br><span class="line">           <span class="keyword">if</span>(!<span class="variable language_">this</span>.<span class="property">_dirty</span>)&#123;</span><br><span class="line">                <span class="variable language_">this</span>.<span class="property">_dirty</span> = <span class="literal">true</span>;</span><br><span class="line">                <span class="comment">// 通知自己收集的effect执行</span></span><br><span class="line">                <span class="title function_">triggerEffects</span>(<span class="variable language_">this</span>.<span class="property">deps</span>)</span><br><span class="line">           &#125;</span><br><span class="line">       &#125;)</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">get</span> <span class="title function_">value</span>()&#123; <span class="comment">// 只有当.value 的时候 dirty为true就执行</span></span><br><span class="line">        <span class="keyword">if</span>(activeEffect)&#123;</span><br><span class="line">            <span class="comment">// 让计算属性做依赖收集</span></span><br><span class="line">            <span class="comment">// 计算属性 -》 effect  set = [effect]</span></span><br><span class="line">            <span class="title function_">trackEffects</span>(<span class="variable language_">this</span>.<span class="property">deps</span> ||( <span class="variable language_">this</span>.<span class="property">deps</span> = <span class="keyword">new</span> <span class="title class_">Set</span>()))</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span>(<span class="variable language_">this</span>.<span class="property">_dirty</span>)&#123;</span><br><span class="line">           <span class="variable language_">this</span>.<span class="property">_dirty</span> = <span class="literal">false</span></span><br><span class="line">           <span class="variable language_">this</span>.<span class="property">_value</span> = <span class="variable language_">this</span>.<span class="property">effect</span>.<span class="title function_">run</span>();</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> <span class="variable language_">this</span>.<span class="property">_value</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">set</span> <span class="title function_">value</span>(<span class="params">newValues</span>)&#123;</span><br><span class="line">        <span class="variable language_">this</span>.<span class="title function_">setter</span>(newValues)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li><p>分支切换</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;!DOCTYPE <span class="keyword">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">html</span> <span class="attr">lang</span>=<span class="string">&quot;en&quot;</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">head</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">&quot;UTF-8&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">meta</span> <span class="attr">http-equiv</span>=<span class="string">&quot;X-UA-Compatible&quot;</span> <span class="attr">content</span>=<span class="string">&quot;IE=edge&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">meta</span> <span class="attr">name</span>=<span class="string">&quot;viewport&quot;</span> <span class="attr">content</span>=<span class="string">&quot;width=device-width, initial-scale=1.0&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">title</span>&gt;</span>Document<span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line">    <span class="comment">&lt;!-- &lt;script src=&quot;../../../node_modules/@vue/reactivity/dist/reactivity.global.js&quot;&gt;&lt;/script&gt; --&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">script</span> <span class="attr">src</span>=<span class="string">&quot;./reactivity.global.js&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">&quot;app&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">script</span>&gt;</span><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">        <span class="comment">// 缓存代理结果 require()</span></span></span><br><span class="line"><span class="language-javascript">        <span class="keyword">const</span> &#123;effect ,reactive&#125; = <span class="title class_">VueReactivity</span>;</span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">        <span class="comment">// 会对属性进行劫持 proxy， 监听用户的获取操作和设置操作</span></span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">        <span class="keyword">const</span> state = <span class="title function_">reactive</span>(&#123; <span class="attr">flag</span>: <span class="literal">true</span>, <span class="attr">name</span>: <span class="string">&#x27;jw&#x27;</span>, <span class="attr">age</span>: <span class="number">30</span> &#125;)</span></span><br><span class="line"><span class="language-javascript">        <span class="title function_">effect</span>(<span class="function">() =&gt;</span> &#123; <span class="comment">// 副作用函数 (effect执行渲染了页面)</span></span></span><br><span class="line"><span class="language-javascript">            <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;render&#x27;</span>)</span></span><br><span class="line"><span class="language-javascript">            <span class="variable language_">document</span>.<span class="property">body</span>.<span class="property">innerHTML</span> = state.<span class="property">flag</span> ? state.<span class="property">name</span> : state.<span class="property">age</span></span></span><br><span class="line"><span class="language-javascript">        &#125;);</span></span><br><span class="line"><span class="language-javascript">        <span class="built_in">setTimeout</span>(<span class="function">() =&gt;</span> &#123;</span></span><br><span class="line"><span class="language-javascript">            state.<span class="property">flag</span> = <span class="literal">false</span>;</span></span><br><span class="line"><span class="language-javascript">            <span class="built_in">setTimeout</span>(<span class="function">() =&gt;</span> &#123;</span></span><br><span class="line"><span class="language-javascript">                <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;修改name，原则上不更新&#x27;</span>)</span></span><br><span class="line"><span class="language-javascript">                state.<span class="property">name</span> = <span class="string">&#x27;zf&#x27;</span></span></span><br><span class="line"><span class="language-javascript">            &#125;, <span class="number">1000</span>);</span></span><br><span class="line"><span class="language-javascript">        &#125;, <span class="number">1000</span>)</span></span><br><span class="line"><span class="language-javascript">    </span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>停止依赖收集</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;!DOCTYPE <span class="keyword">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">html</span> <span class="attr">lang</span>=<span class="string">&quot;en&quot;</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">head</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">&quot;UTF-8&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">meta</span> <span class="attr">http-equiv</span>=<span class="string">&quot;X-UA-Compatible&quot;</span> <span class="attr">content</span>=<span class="string">&quot;IE=edge&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">meta</span> <span class="attr">name</span>=<span class="string">&quot;viewport&quot;</span> <span class="attr">content</span>=<span class="string">&quot;width=device-width, initial-scale=1.0&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">title</span>&gt;</span>Document<span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line">    <span class="comment">&lt;!-- &lt;script src=&quot;../../../node_modules/@vue/reactivity/dist/reactivity.global.js&quot;&gt;&lt;/script&gt; --&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">script</span> <span class="attr">src</span>=<span class="string">&quot;./reactivity.global.js&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">&quot;app&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">script</span>&gt;</span><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">        <span class="comment">// 缓存代理结果 require()</span></span></span><br><span class="line"><span class="language-javascript">        <span class="keyword">const</span> &#123;effect ,reactive&#125; = <span class="title class_">VueReactivity</span>;</span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">        <span class="comment">// 会对属性进行劫持 proxy， 监听用户的获取操作和设置操作</span></span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">        <span class="keyword">const</span> state = <span class="title function_">reactive</span>(&#123; <span class="attr">flag</span>: <span class="literal">true</span>, <span class="attr">name</span>: <span class="string">&#x27;jw&#x27;</span>, <span class="attr">age</span>: <span class="number">30</span> &#125;)</span></span><br><span class="line"><span class="language-javascript">        <span class="comment">// effect返回runner的目的是让用户可以自己控制渲染逻辑</span></span></span><br><span class="line"><span class="language-javascript">        <span class="keyword">const</span> runner = <span class="title function_">effect</span>(<span class="function">() =&gt;</span> &#123; <span class="comment">// 副作用函数 (effect执行渲染了页面)</span></span></span><br><span class="line"><span class="language-javascript">            <span class="variable language_">document</span>.<span class="property">body</span>.<span class="property">innerHTML</span> = state.<span class="property">flag</span> ? state.<span class="property">name</span> : state.<span class="property">age</span></span></span><br><span class="line"><span class="language-javascript">        &#125;);</span></span><br><span class="line"><span class="language-javascript">        runner.<span class="property">effect</span>.<span class="title function_">stop</span>();</span></span><br><span class="line"><span class="language-javascript">        <span class="built_in">setTimeout</span>(<span class="function">() =&gt;</span> &#123;</span></span><br><span class="line"><span class="language-javascript">            state.<span class="property">flag</span> = <span class="literal">false</span>;</span></span><br><span class="line"><span class="language-javascript">            <span class="title function_">runner</span>()</span></span><br><span class="line"><span class="language-javascript">        &#125;, <span class="number">1000</span>)</span></span><br><span class="line"><span class="language-javascript">    </span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>批量更新</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;!DOCTYPE <span class="keyword">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">html</span> <span class="attr">lang</span>=<span class="string">&quot;en&quot;</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">head</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">&quot;UTF-8&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">meta</span> <span class="attr">http-equiv</span>=<span class="string">&quot;X-UA-Compatible&quot;</span> <span class="attr">content</span>=<span class="string">&quot;IE=edge&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">meta</span> <span class="attr">name</span>=<span class="string">&quot;viewport&quot;</span> <span class="attr">content</span>=<span class="string">&quot;width=device-width, initial-scale=1.0&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">title</span>&gt;</span>Document<span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line">    <span class="comment">&lt;!-- &lt;script src=&quot;../../../node_modules/@vue/reactivity/dist/reactivity.global.js&quot;&gt;&lt;/script&gt; --&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">script</span> <span class="attr">src</span>=<span class="string">&quot;./reactivity.global.js&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">&quot;app&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">script</span>&gt;</span><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">        <span class="comment">// 缓存代理结果 require()</span></span></span><br><span class="line"><span class="language-javascript">        <span class="keyword">const</span> &#123;effect ,reactive&#125; = <span class="title class_">VueReactivity</span>;</span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">        <span class="comment">// 会对属性进行劫持 proxy， 监听用户的获取操作和设置操作</span></span></span><br><span class="line"><span class="language-javascript">        <span class="keyword">const</span> state = <span class="title function_">reactive</span>(&#123; <span class="attr">flag</span>: <span class="literal">true</span>, <span class="attr">name</span>: <span class="string">&#x27;jw&#x27;</span>, <span class="attr">age</span>: <span class="number">30</span>, <span class="attr">n</span>:&#123;<span class="attr">n</span>:<span class="number">100</span>&#125;&#125;)</span></span><br><span class="line"><span class="language-javascript">        <span class="comment">// effect返回runner的目的是让用户可以自己控制渲染逻辑</span></span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">        <span class="keyword">let</span> waiting = <span class="literal">false</span></span></span><br><span class="line"><span class="language-javascript">        <span class="keyword">const</span> runner = <span class="title function_">effect</span>(<span class="function">() =&gt;</span> &#123; <span class="comment">// 副作用函数 (effect执行渲染了页面)</span></span></span><br><span class="line"><span class="language-javascript">            <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;runner&#x27;</span>)</span></span><br><span class="line"><span class="language-javascript">            <span class="variable language_">document</span>.<span class="property">body</span>.<span class="property">innerHTML</span> = state.<span class="property">n</span>.<span class="property">n</span>;</span></span><br><span class="line"><span class="language-javascript">        &#125;,&#123;</span></span><br><span class="line"><span class="language-javascript">            <span class="title function_">scheduler</span>(<span class="params"></span>)&#123; <span class="comment">// 调度函数</span></span></span><br><span class="line"><span class="language-javascript">                <span class="keyword">if</span>(!waiting)&#123;</span></span><br><span class="line"><span class="language-javascript">                    waiting = <span class="literal">true</span></span></span><br><span class="line"><span class="language-javascript">                    <span class="title class_">Promise</span>.<span class="title function_">resolve</span>().<span class="title function_">then</span>(<span class="function">()=&gt;</span>&#123;</span></span><br><span class="line"><span class="language-javascript">                        <span class="title function_">runner</span>();</span></span><br><span class="line"><span class="language-javascript">                        waiting = <span class="literal">false</span>;</span></span><br><span class="line"><span class="language-javascript">                    &#125;)</span></span><br><span class="line"><span class="language-javascript">                &#125;</span></span><br><span class="line"><span class="language-javascript">            &#125;</span></span><br><span class="line"><span class="language-javascript">        &#125;);</span></span><br><span class="line"><span class="language-javascript">        <span class="built_in">setTimeout</span>(<span class="function">() =&gt;</span> &#123;</span></span><br><span class="line"><span class="language-javascript">            state.<span class="property">n</span>.<span class="property">n</span> = <span class="number">2000</span>;</span></span><br><span class="line"><span class="language-javascript">        &#125;, <span class="number">1000</span>)</span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">    </span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>computed</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;!DOCTYPE <span class="keyword">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">html</span> <span class="attr">lang</span>=<span class="string">&quot;en&quot;</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">head</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">&quot;UTF-8&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">meta</span> <span class="attr">http-equiv</span>=<span class="string">&quot;X-UA-Compatible&quot;</span> <span class="attr">content</span>=<span class="string">&quot;IE=edge&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">meta</span> <span class="attr">name</span>=<span class="string">&quot;viewport&quot;</span> <span class="attr">content</span>=<span class="string">&quot;width=device-width, initial-scale=1.0&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">title</span>&gt;</span>Document<span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line">    <span class="comment">&lt;!-- &lt;script src=&quot;../../../node_modules/@vue/reactivity/dist/reactivity.global.js&quot;&gt;&lt;/script&gt; --&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">script</span> <span class="attr">src</span>=<span class="string">&quot;./reactivity.global.js&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">&quot;app&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">script</span>&gt;</span><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">        <span class="comment">// 缓存代理结果 require()</span></span></span><br><span class="line"><span class="language-javascript">        <span class="keyword">const</span> &#123;effect ,reactive,computed&#125; = <span class="title class_">VueReactivity</span>;</span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">        <span class="keyword">const</span> state = <span class="title function_">reactive</span>(&#123; <span class="attr">firstName</span>:<span class="string">&#x27;姜&#x27;</span> , <span class="attr">lastName</span>:<span class="string">&#x27;文&#x27;</span> &#125;);</span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">        <span class="comment">// vue2中想更新值  this.xxx = &#x27;&#x27;</span></span></span><br><span class="line"><span class="language-javascript">        <span class="comment">// let fullName = computed(()=&gt;&#123; // dirty 缓存的概念</span></span></span><br><span class="line"><span class="language-javascript">        <span class="comment">//     console.log(&#x27;runner&#x27;)</span></span></span><br><span class="line"><span class="language-javascript">        <span class="comment">//     return state.firstName + state.lastName</span></span></span><br><span class="line"><span class="language-javascript">        <span class="comment">// &#125;);</span></span></span><br><span class="line"><span class="language-javascript">        <span class="keyword">let</span> fullName = <span class="title function_">computed</span>(&#123;</span></span><br><span class="line"><span class="language-javascript">            <span class="title function_">get</span>(<span class="params"></span>)&#123;</span></span><br><span class="line"><span class="language-javascript">                <span class="keyword">return</span> state.<span class="property">firstName</span> + state.<span class="property">lastName</span></span></span><br><span class="line"><span class="language-javascript">            &#125;,</span></span><br><span class="line"><span class="language-javascript">            <span class="title function_">set</span>(<span class="params">value</span>)&#123;</span></span><br><span class="line"><span class="language-javascript">                state.<span class="property">lastName</span> = value</span></span><br><span class="line"><span class="language-javascript">            &#125;</span></span><br><span class="line"><span class="language-javascript">        &#125;)</span></span><br><span class="line"><span class="language-javascript">        fullName.<span class="property">value</span> = <span class="number">100</span>;</span></span><br><span class="line"><span class="language-javascript">        <span class="comment">// 装箱</span></span></span><br><span class="line"><span class="language-javascript">        <span class="title function_">effect</span>(<span class="function">()=&gt;</span>&#123;</span></span><br><span class="line"><span class="language-javascript">            app.<span class="property">innerHTML</span> = fullName.<span class="property">value</span></span></span><br><span class="line"><span class="language-javascript">        &#125;)</span></span><br><span class="line"><span class="language-javascript">      </span></span><br><span class="line"><span class="language-javascript">        <span class="built_in">setTimeout</span>(<span class="function">()=&gt;</span>&#123;</span></span><br><span class="line"><span class="language-javascript">            state.<span class="property">firstName</span> = <span class="string">&#x27;x&#x27;</span></span></span><br><span class="line"><span class="language-javascript">        &#125;,<span class="number">1000</span>);</span></span><br><span class="line"><span class="language-javascript">        <span class="comment">// 计算属性就是一个effect 当我取值的时候 会访问firsName，lastName的值，会让firstName和lastKName 来收集当前的computedEffect</span></span></span><br><span class="line"><span class="language-javascript">        <span class="comment">// 计算属性也可以收集effect</span></span></span><br><span class="line"><span class="language-javascript">        <span class="comment">//  effect</span></span></span><br><span class="line"><span class="language-javascript">        <span class="comment">// effectComputed</span></span></span><br><span class="line"><span class="language-javascript">        <span class="comment">// proxy</span></span></span><br><span class="line"><span class="language-javascript">    </span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre></td></tr></table></figure></li>
</ul>

      
    </div>
    <footer class="article-footer">
      <a data-url="https://gitee.com/wieldy/bailment-blog/2022/03/29/vue3-1.reactivity/" data-id="cljmgj1f0000pzc9jhnk8ce5q" data-title="1.reactivity" class="article-share-link">Share</a>
      
      
      
    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/bailment-blog/2022/03/29/stream/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          stream
        
      </div>
    </a>
  
  
</nav>

  
</article>


</section>
        
          <aside id="sidebar">
  
    

  
    

  
    
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/bailment-blog/archives/2023/07/">July 2023</a></li><li class="archive-list-item"><a class="archive-list-link" href="/bailment-blog/archives/2023/06/">June 2023</a></li><li class="archive-list-item"><a class="archive-list-link" href="/bailment-blog/archives/2023/04/">April 2023</a></li><li class="archive-list-item"><a class="archive-list-link" href="/bailment-blog/archives/2023/03/">March 2023</a></li><li class="archive-list-item"><a class="archive-list-link" href="/bailment-blog/archives/2022/03/">March 2022</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/bailment-blog/2023/07/03/%E5%B7%A5%E4%BD%9C%E6%97%A5%E5%BF%97/">工作日志</a>
          </li>
        
          <li>
            <a href="/bailment-blog/2023/06/29/payMent/">payMent</a>
          </li>
        
          <li>
            <a href="/bailment-blog/2023/06/20/%E7%BD%91%E7%BA%A2%E9%9D%A2%E8%AF%95%E9%A2%98/">原理题</a>
          </li>
        
          <li>
            <a href="/bailment-blog/2023/06/19/%E5%89%8D%E7%AB%AF%E5%A4%A7%E5%B1%8F%E8%87%AA%E9%80%82%E5%BA%94%E6%96%B9%E6%B3%95/">前端大屏自适应方法</a>
          </li>
        
          <li>
            <a href="/bailment-blog/2023/06/17/10%E4%B8%AAJavaScript%E5%B8%B8%E8%A7%81%E9%AB%98%E7%BA%A7%E7%9F%A5%E8%AF%86%E7%82%B9/">10个JavaScript常见高级知识点</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      
      &copy; 2023 John Doe<br>
      Powered by <a href="https://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>

    </div>
    <nav id="mobile-nav">
  
    <a href="/bailment-blog/" class="mobile-nav-link">Home</a>
  
    <a href="/bailment-blog/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    


<script src="/bailment-blog/js/jquery-3.4.1.min.js"></script>



  
<script src="/bailment-blog/fancybox/jquery.fancybox.min.js"></script>




<script src="/bailment-blog/js/script.js"></script>





  </div>
</body>
</html>